CategoryController = {
	categoryModel : null,
	page: 0,
	pos: 0,
	pageTotal:0,
	limit: 48,
	items: 12,	
	
	init: function() {	
		this.view = CategoryView;		
        this.isFirst = true;
		this.model = new ListBook();
		this.categoryModel = CategoryModel;
		this.callServiceCategory();
		this.callServiceBooks();
	},
	
	callServiceBooks : function(categoryID,keyWord){		
		 var self = this;
		 var callService = function () {
		 	ajax.get({
				 url : "http://localhost:8080/book-service/public/book/store/allbook?",
				 param : {
					 "start" : self.page * self.items,
					 "limit" : self.limit,
					 "categoryId" : categoryID != null ? categoryID : 0,
                     "keyWord": keyWord
				 },
				 success : function(data, xhr) {
		    	    var status = parseInt(data.childNodes[0].getAttributeNode("status").nodeValue);
	                    if(status == -1){
                            alert("Không có sách!!");
                        }else {
                            var count = parseInt(data.childNodes[0].getAttributeNode("countTotal").nodeValue);
                            if(count%self.items == 0){
	                            self.pageTotal = parseInt(count/self.items);
	                        }else{
	                            self.pageTotal = parseInt(count/self.items) + 1;
	                        }
                        }
					self.isFinishBook = true; 
					self.pos = 0;               
					self.model.Books = self.categoryModel.createBooks(data);
					self.updateData();
				 },
				 failure : function(xhr) {
					 console.log("fail");
				 }
			 });
		};		
		
		if(this.page % 4 == 0 || this.pos > this.model.Books.length - 1) {
			pagesManager.showLoading();			
			callService();
		} else {
			this.view.updateView();
		}		
	},
	
	callServiceCategory: function() {
		var self = this;
		
		ajax.get({
			 url : "http://localhost:8080/book-service/public/book/store/category",
			 
			 success : function(data, xhr) {
			 	self.isFinishCategory = true;
			 	self.model.Categories = self.categoryModel.createCategories(data);
				self.updateData();
			 },
			 failure : function(xhr) {
				 console.log("fail");
			 }
		 });		
	},
	
	updateData: function() {
		if(this.isFinishCategory && this.isFinishBook) {			
			this.isFinishBook = false;
			this.view.updateView();
			pagesManager.hideLoading();
		}
	},
	
	callNext: function() {
		if(this.page != this.pageTotal - 1) {
			this.page++;
			this.pos++;
			this.callServiceBooks();
		}
	},
	
	callPrevious: function() {
		if(this.page != 0) {
			this.page--;
			this.pos--;
			this.callServiceBooks();	
		}
	},

    callFirst: function() {
		if(this.page != 0) {
			this.page = 0;
			this.pos = 0;
			this.callServiceBooks();
		}
	},

    callLast: function() {
		if(this.page != this.pageTotal - 1) {
			this.page = this.pageTotal - 1;
			this.pos = this.page;
			this.callServiceBooks();
		}
	},    

    onClick: function(node) {
        switch(node) {
            case "categoryFirst":
                this.callFirst();
                break;
            case "categoryLast":
                this.callLast();
                break;
            case "categoryPrevious":
                this.callPrevious();
                break;
            case "categoryNext":
                this.callNext();
                break;
            case "btnSearch":
                var key = $id("searchBar").value;
                this.callServiceBooks(0,key);
                break;
        }
    }
};
